layui.use(['form','layer','layedit','laydate','upload'],function(){
    var form = layui.form
    layer = parent.layer === undefined ? layui.layer : top.layer,
        laypage = layui.laypage,
        upload = layui.upload,
        layedit = layui.layedit,
        laydate = layui.laydate,
        $ = layui.jquery;

    laydate.render({
        elem: '#test1' //指定元素
    });
    form.on('select(test)', function(data){
        var className ='stop_'+(data.value);
        var tips  = $('.'+className).attr('tips');
        $('#reason_content').val(tips);
        //data.value
    });

    form.on("submit(submit)",function(data){
        var addUrl = $('#addUrl').val();
        $.ajax({
            type: "Post",
            url: addUrl,
            dataType: "json",
            data:data.field,
            success: function(msg){
                if(msg.code == '-1'){
                    layer.msg(msg.msg,{time:0.6*1000});
                }
                if(msg.code =='1'){

                    window.parent.location.reload();
                    var index = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(index);
                }
            }
        });
        return false;
    })

    //多图片上传
    upload.render({
        elem: '#test2'
        ,url: '/admin/Upload/uploadImg'
        ,multiple: true
        ,before: function(obj){
            //预读本地文件示例，不支持ie8
            obj.preview(function(index, file, result){
                $('#demo2').append('<img style="width: 85px;height: 85px;margin: 5px 5px 5px 5px" src="'+ result +'" alt="'+ file.name +'" class="layui-upload-img"> ')
            });
        }
        ,done: function(res){
            //上传完毕
            var html = $('#imgID').html();
            html+= '<input type="hidden" name ="img_id[]" value="'+res.img+'">';
            $('#imgID').html(html);
        }
    });
    //多图片上传
    upload.render({
        elem: '#notThumbnail'
        ,url: '/admin/Upload/notThumbnail'
        ,multiple: true
        ,before: function(obj){
            //预读本地文件示例，不支持ie8
            obj.preview(function(index, file, result){
                $('#demo2').append(
                    '<img style="width: 85px;height: 85px;margin: 5px 5px 5px 5px" src="'+ result +'" alt="'+ file.name +'" class="layui-upload-img"> '
                );
            });
        }
        ,done: function(res){
            //上传完毕
            $('#demo2').append('<a href="javascript:;" class = "delFile" data-id ='+res.img+'  onclick="delFile(this)" >删除</a>');
            var html = $('#imgID').html();
            html+= '<input type="hidden" name ="new_img_id[]" value="'+res.img+'">';
            $('#imgID').html(html);
        }
    });


})

function delFile (data) {
    console.log(data);
    var id =  $(data).attr('data-id');
    $.ajax({
        type: "Post",
        url: '/admin/Upload/delFile',
        dataType: "json",
        data:'imgId='+id,
        success: function(msg){
            if(msg.code == '-1'){
                layer.msg(msg.msg,{time:0.6*1000});
            }
            if(msg.code =='1'){
                $(data).prev().remove();
                $(data).remove();
            }
        }
    });
}


function delImg (data) {
    console.log(data);
    var id =  $(data).attr('data-id');
    var ids =  $(data).attr('data-ids');
    $.ajax({
        type: "Post",
        url: '/admin/Upload/delFile',
        dataType: "json",
        data:'imgId='+id,
        success: function(msg){
            if(msg.code == '-1'){
                layer.msg(msg.msg,{time:0.6*1000});
            }
            if(msg.code =='1'){
                $(data).prev().prev().remove();
                $(data).prev().remove();
                $(data).remove();
                var val = $('#del_img_id').val();
                var idArray ='';
                if(val == ''){
                    idArray =ids;
                }else{
                    idArray = val+','+ids;
                }
                $('#del_img_id').val(idArray);

            }
        }
    });
}